package com.yuanfeng.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.payment.entity.PayConfigShopEntity;
import com.yuanfeng.payment.mapper.PayConfigShopMapper;
import com.yuanfeng.payment.service.PayConfigShopService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Service
public class PayConfigShopServiceImpl extends ServiceImpl<PayConfigShopMapper, PayConfigShopEntity>
    implements PayConfigShopService{
    @Value(value = "${firstLevel}")
    private String firstLevel;

    @Override
    public ResponseResult select(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {

            //根据shopId查询小程序
            PayConfigShopEntity entity= this.baseMapper.selectpaymentconfig(userInfo.getShopId());
            return ResponseResult.success(entity);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult update(Map<String, Object> map) {
        String token = String.valueOf(map.get("token"));
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {

            map.put("shopId", userInfo.getShopId());
            map.put("refundUrl", "https://api.mch.weixin.qq.com/secapi/pay/refund");
            map.put("notifyUrl", firstLevel + "/mall-payment/pay/WXPay/notifyWeiXinPay");
            map.put("redirectUrl", firstLevel + "/h5/#/member/orderlist/orderlist");
            map.put("appletNotifyUrl", firstLevel + "/mall-payment/pay/WXPay/notifyWeiXinPayByApplet");
            Integer integer = null;
            if (map.get("id") != null && map.get("id") != "") {
                integer = this.baseMapper.updatepaymentconfig(map);
            } else {
                integer = this.baseMapper.insertpaymentconfig(map);
            }


            if (integer != null) {
                return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
            }
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




